#include "client.h"

/*初始化连接，成功返回1，失败返回0*/
int connectInit(int* pSockFd, char* ip, int port){
    *pSockFd = socket(AF_INET, SOCK_STREAM, 0);
    ERROR_CHECK(*pSockFd, -1, "socket");

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip);

    int ret = connect(*pSockFd, (struct sockaddr*)&addr, sizeof(addr));
    if(ret == -1){
        printf("连接失败！\n");
        return 0;
    }
    printf("连接成功！\n");
    return 1;
}

int myShell(const int sockFd){
    int msgLength = 0;
    char msgBuf[BUFFERSIZE] = {0};
    
    //登录
    printf("请选择登录或注册:\n");
    printf("1. 登录\n");
    printf("2. 注册\n");
    printf("3. 退出\n");
    int choice = 0;
    scanf("%d", &choice);
    switch (choice)
    {
    case 1:
        strcpy(msgBuf, "登录");
        msgLength = strlen(msgBuf);
        sendn(sockFd, &msgLength, sizeof(int));
        sendn(sockFd, msgBuf, msgLength);
        break;
    case 2:
        strcpy(msgBuf, "注册");
        msgLength = strlen(msgBuf);
        sendn(sockFd, &msgLength, sizeof(int));
        sendn(sockFd, msgBuf, msgLength);
        break;
    case 3:
        printf("再见!\n");
        exit(0);
        break;
    default:
        break;
    }
}